const clubService = require('../service/club.service')
const {
	CREATE_CLUB_ERROR,
	AUIT_FAILED,
	ADD_CLUD_FAILED,
	DELETE_CLUB_FAILED
} = require('../config/error')

class ClubController {
	async create(ctx, next) {
		// 1.获取参数
		const clubInfo = ctx.request.body
		console.log(clubInfo)

		try {
			// 2.操作数据库
			const result = await clubService.create(clubInfo)

			ctx.body = {
				code: 0,
				message: '创建社团成功'
			}
		} catch (err) {
			ctx.app.emit('error', CREATE_CLUB_ERROR, ctx)
		}
	}
	// 获取所有社团
	async list(ctx, next) {
		const { limit, offset, status } = ctx.query
		console.log(`output->`, limit)
		const result = await clubService.listClubs(limit, offset, status)
		ctx.body = { code: 0, message: '查询所有社团成功', data: result }
	}

	// 获取一个社团的信息
	async findOne(ctx, next) {
		const id = ctx.params.id
		const result = await clubService.findClubById(id)
		ctx.body = result
			? { code: 0, message: '查询单个社团成功', data: result }
			: { code: -3005, message: '社团不存在' }
	}

	// 根据名字查询社团
	async findOneByName(ctx, next) {
		const { name, limit, offset, status } = ctx.query
		const result = await clubService.findClubByNameLike(
			name,
			limit,
			offset,
			status
		)
		ctx.body = {
			code: 0,
			message: '查询成功',
			data: result
		}
	}

	// 删除社团
	async delete(ctx, next) {
		const id = ctx.params.club_id
		const res = await clubService.deleteClub(id)
		if (!res.affectedRows) {
			ctx.app.emit('error', DELETE_CLUB_FAILED, ctx)
			return
		}
		ctx.body = { code: 0, message: '删除社团成功' }
	}

	// 更新社团
	async update(ctx, next) {
		const { club_id } = ctx.params
		const clubInfo = ctx.request.body
		await clubService.updateClub(club_id, clubInfo)
		ctx.body = { code: 0, message: '更新社团信息成功' }
	}

	// 更新社团状态
	async updateClubStatus(ctx, next) {
		const { club_id } = ctx.params
		const { status } = ctx.query
		await clubService.updateClubStatus(club_id, status)
		ctx.body = {
			code: 0,
			message: '修改状态成功'
		}
	}

	// 获取社团成员列表
	async getMembers(ctx, next) {
		const { club_id } = ctx.params // 获取路由参数中的club_id
		const { limit, offset, name, status } = ctx.query
		console.log(club_id, limit)
		const members = await clubService.getClubMembersByClubIdAndStatus(
			club_id,
			limit,
			offset,
			name,
			status
		)
		ctx.body = {
			code: 0,
			message: '获取社团成员列表成功',
			data: members
		}
	}

	// 审核社团成员
	async reviewMember(ctx, next) {
		const { club_id, user_id } = ctx.params // 获取路由参数中的club_id和user_id
		const { status } = ctx.request.body // 获取请求体中的status
		console.log(club_id)
		if (!['pending', 'active', 'rejected'].includes(status)) {
			ctx.body = { code: 1, message: '无效的状态' }
			return
		}
		try {
			const result = await clubService.updateMemberStatus(
				club_id,
				user_id,
				status
			)
			const message = status === 'active' ? '成员审核通过' : '成员审核未通过'
			ctx.body = { code: 0, message }
		} catch (err) {
			ctx.app.emit('error', AUIT_FAILED, ctx) // 触发错误事件，应该在app中统一处理错误
		}
	}

	// 用户申请加入社团
	async join(ctx, next) {
		const { club_id } = ctx.params // 从URL路径中获取社团ID
		const { id } = ctx.user // 从请求体中获取用户ID
		console.log(club_id, id)
		// 这里应该有身份验证和授权逻辑，确认用户已登录并获取其user_id
		// const user_id = ctx.state.user.id;

		try {
			const response = await clubService.joinClub(club_id, id)
			if (response.alreadyExists) {
				ctx.body = { code: 1, message: '用户已在社团中或已提交申请' }
			} else {
				ctx.body = { code: 0, message: '申请加入社团成功' }
			}
		} catch (err) {
			ctx.app.emit('error', ADD_CLUD_FAILED, ctx) // 触发错误事件，应该在app中统一处理错误
		}
	}

	// 获取待审核的社团成员列表
	async getPendingMembers(ctx, next) {
		const { club_id } = ctx.params
		console.log(club_id)
		// 这里应该有身份验证和授权逻辑，确保请求者是社团社长
		const pendingMembers = await clubService.getPendingClubMembers(club_id)
		ctx.body = {
			code: 0,
			message: '获取待审核的社团成员列表成功',
			data: pendingMembers
		}
	}

	// 获取已审核的社团成员列表
	async getReviewedMembers(ctx, next) {
		const { club_id } = ctx.params

		const reviewedMembers = await clubService.getReviewedClubMembers(club_id)
		ctx.body = {
			code: 0,
			message: '获取已审核的社团成员列表成功',
			data: reviewedMembers
		}
	}

	// 查询参加了哪些社团
	async getClubsForUser(ctx, next) {
		const { id } = ctx.params ?? ctx.user
		// 查询
		const clubs = await clubService.queryClubsForUser(id)
		ctx.body = {
			code: 0,
			message: '查询成功',
			data: clubs
		}
	}
	// 根据社团id和用户id删除用户
	async deleteClubMember(ctx, next) {
		const { club_id, user_id } = ctx.params
		const res = await clubService.removeClubMemberById(club_id, user_id)
		ctx.body = {
			code: 0,
			message: '删除成员成功'
		}
	}
	// 根据社长id查询社团
	async queryClubByUserId(ctx, next) {
		const { id } = ctx.user
		const res = await clubService.queryClubByUserId(id)
		ctx.body = {
			code: 0,
			message: '获取社团成功',
			data: res
		}
	}

	// 根据用户id查询用户状态
	async queryStatusById(ctx, next) {
		const { id } = ctx.user
		const { club_id } = ctx.params
		const res = await clubService.queryStatusById(id, club_id)
		ctx.body = {
			code: 0,
			message: '获取用户状态成功',
			data: res
		}
	}

	// 退出社团
	async quitClub(ctx, next) {
		const { id } = ctx.user
		const { club_id } = ctx.params
		const res = await clubService.removeClubMemberById(club_id, id)
		ctx.body = {
			code: 0,
			message: '退出社团成功',
		}
	}

	// 查询社团人数
	async queryJoinCount(ctx, next) {
		const res = await clubService.queryJoinCount()
		ctx.body = {
			code: 0,
			message: '查询成功',
			data: res
		}
	}
}

module.exports = new ClubController()
